"use client"
import { getDicts } from "@/clientApi/system/dict/data";
import { RootState, useAppDispatch } from "@/store/client";
import { setDict } from "@/store/client/modules/dictStore";
import { useEffect } from "react";
import { useSelector } from "react-redux";


const useDict = (...args: any[]) => {
    const dispatch = useAppDispatch()
    const dicts = useSelector((state: RootState) => state.dict.dictData);
    useEffect(() => {
        args.forEach((dictType) => {
            // 检查是否已经存在该字典类型的数据
            if (!dicts.some((item) => item.keys === dictType)) {
                getDicts(dictType).then(resp => {
                    const res = resp.data.map((p: any) => ({
                        label: p.dictLabel,
                        value: p.dictValue,
                        elTagType: p.listClass,
                        elTagClass: p.cssClass
                    }));
                    dispatch(setDict({ _key: dictType, value: res }));
                });
            }
        });
    }, [args.join(',')]); // 依赖 args 的变化

    // 返回当前已有的字典数据
    return dicts;
}
export default useDict;


// 使用方式
//注意不可放到useEffect中
// const dicts = useDict('sys_user_sex');